from flask import session, current_app, g

from functools import wraps


def user_login_check(viewfunc):
    # 获取到当前登录用户的id
    @wraps(viewfunc)
    def check(*args, **keyargs):
        user_id = session.get("userid")
        # 通过id获取用户信息
        user = None
        if user_id:
            from app.models import User

            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)

        g.user = user
        return viewfunc(*args, **keyargs)

    return check
